package com.pactera.android.activity;

import com.pactera.android.R;
import com.pactera.android.R.id;
import com.pactera.android.R.layout;
import com.pactera.android.R.menu;
import com.pactera.android.application.PacteraApplication;
import com.pactera.android.version.CheckVersionTask;
import com.pactera.android.version.UpdateManager;

import android.net.http.SslError;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.SslErrorHandler;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.TextView;

public class ContactListWebviewActivity extends BaseActivity {
	
	static final String TAG = "ContactListActivity";
	
    private WebView webView;
    
    private String URL = "http://54.252.87.196:8080/address/linkAction!index";
    
    
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {	
		super.onCreate(savedInstanceState);				
		CheckVersionTask.checkVersion(this);
		setContentView(R.layout.contact_list_webview);
		webView = new WebView(this);
				
		webView = (WebView) findViewById(R.id.webView1);
		webView.getSettings().setJavaScriptEnabled(true);
		webView.requestFocus();
		
		webView.setWebViewClient(new WebViewClient(){
						
			/* (non-Javadoc)
			 * @see android.webkit.WebViewClient#onReceivedError(android.webkit.WebView, int, java.lang.String, java.lang.String)
			 */
			@Override
			public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {				
				super.onReceivedError(view, errorCode, description, failingUrl);
				view.loadData(errorHtml, "text/html", "UTF-8"); 
			}

			@Override
			public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
				handler.proceed();				
			}

			@Override
		    public boolean shouldOverrideUrlLoading(WebView view, String url) { 
				view.loadUrl(url);
		       return true;
		  }
									
		});
		
		
		
		webView.loadUrl(URL);
	}
	
	
	
	/* (non-Javadoc)
	 * @see android.app.Activity#onKeyDown(int, android.view.KeyEvent)
	 */
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (webView.canGoBack() && keyCode == KeyEvent.KEYCODE_BACK) {
			webView.goBack(); 

			return true;
		}
		return false;
	}
	
	/* (non-Javadoc)
	 * @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem)
	 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		super.onOptionsItemSelected(item);
		
		 switch (item.getItemId()) {
	        case R.id.refresh:	        	
	            this.webView.loadUrl(URL);
	            return true;	           
	        default:
	            return super.onOptionsItemSelected(item);
		 }
	}
	
	
	 private void checkUpdate() {
	    	UpdateManager updateManager = new UpdateManager(this);				
			Thread thread = new Thread(updateManager);
			if (Thread.State.NEW == thread.getState()) {
				thread.start();				
			} 	
	    }
	
	
	
	
	
	
}
